// ====================Scene_CardGame====================

class Scene_CardGame extends Scene_Base {
    create() {
        this.createContainer();
        this.createDisplayObjects();
        CardGameManager.setup();
    }

    createContainer() {
        this.createBackgroundContainer();
        this.createEnemyContainer();
        this.createActorContainer();
        this.createUIContainer();
        this.createAnimationContainer();
    }

    createBackgroundContainer() {
        const container = new CardContainerBase();
        this._backgroundContainer = container;
        this.addChild(container);
    }

    createEnemyContainer() {
        const container = new CardContainerBase();
        this._enemyContainer = container;
        this.addChild(container);
    }

    createActorContainer() {
        const container = new CardContainerBase();
        this._actorContainer = container;
        this.addChild(container);
    }

    createUIContainer() {
        const container = new CardContainerBase();
        this._uiContainer = container;
        this.addChild(container);
    }

    createAnimationContainer() {
        const container = new CardContainerBase();
        this._animationContainer = container;
        this.addChild(container);
        CardGameManager.setAnimationContainer(this._animationContainer);
    }

    createDisplayObjects() {
        this.createBackground();
        this.createEnemy();
        this.createActor();
        this.createAllCardGameEle();
    }

    createBackground() {
        const backSprite1 = new Sprite(ImageManager.loadBattleback1("Desert"));
        const backSprite2 = new Sprite(ImageManager.loadBattleback2("Desert"));
        this._backSprite1 = backSprite1;
        this._backSprite2 = backSprite2;
        this._backgroundContainer.addChild(backSprite1);
        this._backgroundContainer.addChild(backSprite2);
        this.adjustBackgroundPosition()
    }

    adjustBackgroundPosition() {
        const sprites = [this._backSprite1, this._backSprite2];
        for (const sprite of sprites) {
            const bitmap = sprite.bitmap;
            bitmap.addLoadListener(() => {
                const {width, height} = bitmap
                const ratioX = Graphics.width / bitmap.width;
                const ratioY = Graphics.width / bitmap.height;
                const scale = Math.max(ratioX, ratioY, 1.0);
                // const x = (Graphics.width - bitmap.width) / 2;
                // const y = 0;
                sprite.scale.set(scale, scale)
                // sprite.move(x,y);
            })
        }
    }

    createEnemy() {
        const enemies = $gameTroop.members();
        const sprites = [];
        for (const enemy of enemies) {
            sprites.push(new Sprite_Enemy(enemy));
        }
        // sprites.sort(this.compareEnemySprite.bind(this));
        for (const sprite of sprites) {
            this._enemyContainer.addChild(sprite);
        }
        this._enemySprites = sprites;
    }

    createActor() {
        const actors = $gameParty.members()[0];
        const sprites = [];
        // for (const actor of actors) {
        //     sprites.push(new Sprite_Actor(actor));
        // }
        sprites.push(new Sprite_Actor($gameParty.members()[0]))
        for (const sprite of sprites) {
            this._actorContainer.addChild(sprite);
        }
        this._actorSprites = sprites;
    }


    createAllCardGameEle() {
        this.createCardPanel();
        this.createCardEffectPanel();
        this.createDeckSprite();
        this.createCemeterySprite();
        this.createEnergySprite();
        this.createButton();
        this.createDisplayWindow();
        this.createRewardWindow();
        this.createTipWindow();
    }

    /**
     * 创建手牌面板
     */
    createCardPanel() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.handCardRect;
        const {x, y, width, height} = rect;
        const cardPanel = new HandCardContainer({
            width,
            height,
            maxAmount: SDS_CARD_PLUGIN_PARAM.gameOption.maxHandCard,
            libraryX: Graphics.boxWidth + 200,
            libraryY: Graphics.boxHeight
        });
        this._cardPanel = cardPanel;
        this._uiContainer.addChild(cardPanel);
        cardPanel.move(x, y);
        cardPanel.updateTransform();
        CardGameManager.setCardPanel(this._cardPanel);
    }

    /**
     * 创建效果面板
     */
    createCardEffectPanel() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.effectRect;
        const {x, y, width, height} = rect;
        const selectPanel = new EffectCardContainer({
            width,
            height
        });
        selectPanel.hide();
        this._cardEffectPanel = selectPanel;
        this._uiContainer.addChild(selectPanel);
        selectPanel.move(x, y);
        CardGameManager.setCardEffectPanel(this._cardEffectPanel);
        this._cardEffectPanel.setHandPanel(this._cardPanel);
        this._cardPanel.setEffectPanel(this._cardEffectPanel);
    }


    /**
     * 创建牌堆精灵
     */
    createDeckSprite() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.deckRect;
        const {x, y, width, height} = rect;
        const type = "deck";
        const deckFile = SDS_CARD_PLUGIN_PARAM.gameUI.deckFile;
        const fontSize = SDS_CARD_PLUGIN_PARAM.gameUI.deckFontSize;
        const iconSize = SDS_CARD_PLUGIN_PARAM.gameUI.deckIconSize;
        const iconIndex = SDS_CARD_PLUGIN_PARAM.gameUI.deckIconIndex;
        const sprite = new Sprite_CardDeck({width, height, type, fontSize, iconSize, iconIndex, deckFile});
        sprite.move(x, y);
        this._deckSprite = sprite;
        this._uiContainer.addChild(sprite);
        // CardGameManager.setDeckSprite(this._deckSprite);
        CardGameManager.deckPosition = new Point(x + width / 2, y + height);
    }

    /**
     * 创建墓地精灵
     */
    createCemeterySprite() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.cemeteryRect;
        const {x, y, width, height} = rect;
        const type = "cemetery";
        const deckFile = SDS_CARD_PLUGIN_PARAM.gameUI.deckFile;
        const fontSize = SDS_CARD_PLUGIN_PARAM.gameUI.deckFontSize;
        const iconSize = SDS_CARD_PLUGIN_PARAM.gameUI.deckIconSize;
        const iconIndex = SDS_CARD_PLUGIN_PARAM.gameUI.cemeteryIconIndex;
        const sprite = new Sprite_CardDeck({width, height, type, fontSize, iconSize, iconIndex, deckFile});
        sprite.move(x, y);
        this._cemeterySprite = sprite;
        this._uiContainer.addChild(sprite);
        CardGameManager.cemeteryPosition = new Point(x + width / 2, y + height);
    }

    /**
     * 创建能量精灵
     */
    createEnergySprite() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.energyRect;
        const fullIconIndex = SDS_CARD_PLUGIN_PARAM.gameUI.fullEnergyIconIndex;
        const emptyIconIndex = SDS_CARD_PLUGIN_PARAM.gameUI.emptyEnergyIconIndex;
        const {x, y, width, height} = rect;
        const sprite = new CardEnergy({width, height, fullIconIndex, emptyIconIndex});
        sprite.move(x, y);
        this._uiContainer.addChild(sprite);
        this._energySprite = sprite;
    }

    /**
     * 创建按钮
     */
    createButton() {
        const rect = SDS_CARD_PLUGIN_PARAM.gameUI.turnButtonRect;
        const {x, y, width, height} = rect;
        // TODO 临时
        const bitmap = new Bitmap(width, height * 2);
        const h = Math.floor(height / 2);
        const point1 = new Point(0, h);
        const point2 = new Point(h, 0);
        const point3 = new Point(width - h, 0);
        const point4 = new Point(width, h);
        const point5 = new Point(width - h, height);
        const point6 = new Point(h, height);
        const points1 = [point1, point2, point3, point4, point5, point6];
        const point7 = new Point(0, h * 3);
        const point8 = new Point(h, height);
        const point9 = new Point(width - h, height);
        const point10 = new Point(width, height + h);
        const point11 = new Point(width - h, height * 2);
        const point12 = new Point(h, height * 2);
        const points2 = [point7, point8, point9, point10, point11, point12];
        bitmap.fontFace = $gameSystem.mainFontFace();
        bitmap.fontSize = Math.min(height - 4, (width / 4) - 4);
        bitmap.fillPolygon(points1, "#b425d7");
        bitmap.fillPolygon(points2, "#c2c2c2");
        bitmap.drawText("回合结束", 0, 0, width, height, "center");
        bitmap.drawText("回合结束", 0, height, width, height, "center");
        const text = bitmap;
        const func = CardGameManager.cardTurnEnd.bind(CardGameManager);
        const button = new Sprite_CardButton(x, y, text, func);
        this._turnButton = button;
        this._uiContainer.addChild(button);
    }

    /**
     * 创建卡牌展示窗口
     */
    createDisplayWindow() {
        const rect = new Rectangle(0, 0, Graphics.width, Graphics.height);
        const row = 2;
        const col = 5;
        const {x, y, width, height} = rect;
        const window = new CardDisplay({width, height, row, col});
        window.move(x, y);
        this._uiContainer.addChild(window);
        this._displayWindow = window;
        CardGameManager.setDisplayWindow(window);
    }

    /**
     * 创建奖励窗口
     */
    createRewardWindow() {
        const rect = new Rectangle(600, 150, 240, 600);
        const {x, y, width, height} = rect;
        const window = new Window_CardReward({width, height});
        window.move(x, y);
        this._uiContainer.addChild(window);
        this._rewardWindow = window;
        CardGameManager.setRewardWindow(window);
    }

    /**
     * 创建提示窗口
     */
    createTipWindow() {
        const rect = new Rectangle(0, 150, Graphics.width, 50);
        const {x, y, width, height} = rect;
        const window = new Window_Tip({width, height});
        window.move(x, y);
        this._uiContainer.addChild(window);
        this._tipWindow = window;
        CardGameManager.setTipWindow(window);
    }

    actorSprite() {
        return this._actorSprites;
    }

    enemySprite() {
        return this._enemySprites;
    }

    battlerSprite() {
        return this._enemySprites.concat(this._actorSprites);
    }

    update() {
        super.update();
        CardGameManager.updatePhase();
    }

    popScene() {
        $gameParty.onBattleEnd();
        $gameTroop.onBattleEnd();
        super.popScene();
    }
}